﻿using AppSocketModel;
using AppSocketService.Device;
using BCGPSCommon;
using BCGPSMongoData;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace AppSocketControl.GpsData.DataDispatch
{
    public  class DispatchRstTypeCnt
    {
        private readonly MongoDBCommand<DeviceRstTypeCnt> mongoDBCommand;
        private DeviceRstTypeCnt  deviceRstTypeCnt = new DeviceRstTypeCnt(); 
        public DispatchRstTypeCnt()
        {
            mongoDBCommand = new MongoDBCommand<DeviceRstTypeCnt>("DeviceRstTypeCount");

        }

        private void Split(string[] dataArr)
        {
            deviceRstTypeCnt = new DeviceRstTypeCnt();
            deviceRstTypeCnt.HardwareVersion = dataArr[1];
            deviceRstTypeCnt.HardwareCode = dataArr[2];
            deviceRstTypeCnt.FirmwareVersion = dataArr[3];
            deviceRstTypeCnt.FirmwareCode = dataArr[4];
            deviceRstTypeCnt.IMEI = dataArr[5];
            Msg.WirteLine(deviceRstTypeCnt.IMEI, 10);
            deviceRstTypeCnt.ReceiveDateTime = MongoDBDateTime.GetNowUtc();
            

            var arr2 = dataArr[7].Split(',');
            if (arr2.Length > 0)
            {
                foreach (var item in arr2)
                {
                    Msg.WirteLine(item, 10);
                    string[] itemArr = Regex.Split(item, ":=", RegexOptions.IgnoreCase);
                    if (itemArr.Length > 1)
                    {
                        try
                        {
                            switch (itemArr[0])
                            {
                                case "1":
                                case "OnSignal":
                                    deviceRstTypeCnt.OnSignal = int.Parse(itemArr[1]);
                                    break;
                                case "2":
                                case "OnReadImei":
                                    deviceRstTypeCnt.OnReadImei = int.Parse(itemArr[1]);
                                    break;
                                case "3":
                                case "OnGsmInit":
                                    deviceRstTypeCnt.OnGsmInit = int.Parse(itemArr[1]);
                                    break;
                                case "4":
                                case "OnReadCard":
                                    deviceRstTypeCnt.OnReadCard = int.Parse(itemArr[1]);
                                    break;
                                case "5":
                                case "OnGprsInit":
                                    deviceRstTypeCnt.OnGprsInit = int.Parse(itemArr[1]);
                                    break;
                                case "6":
                                case "OnSetLbsCfg":
                                    deviceRstTypeCnt.OnSetLbsCfg = int.Parse(itemArr[1]);
                                    break;
                                case "7":
                                case "OnUdpLink":
                                    deviceRstTypeCnt.OnUdpLink = int.Parse(itemArr[1]);
                                    break;
                                case "8":
                                case "OnSendDeviceInfo":
                                    deviceRstTypeCnt.OnSendDeviceInfo = int.Parse(itemArr[1]);
                                    break;
                                case "9":
                                case "OnGetLbs":
                                    deviceRstTypeCnt.OnGetLbs = int.Parse(itemArr[1]);
                                    break;
                                case "10":
                                case "OnGetGps":
                                    deviceRstTypeCnt.OnGetGps = int.Parse(itemArr[1]);
                                    break;
                                case "11":
                                case "OnSendData":
                                    deviceRstTypeCnt.OnSendData = int.Parse(itemArr[1]);
                                    break;
                                case "12":
                                case "BatVoltage":
                                    deviceRstTypeCnt.BatteryVoltage = float.Parse(itemArr[1]);
                                    break;
                                case "13":
                                case "OnModeToAT":
                                    deviceRstTypeCnt.OnModeToAT = int.Parse(itemArr[1]);
                                    break;
                                case "14":
                                case "OnReadGprsModuleName":
                                    deviceRstTypeCnt.OnReadGprsModuleName = int.Parse(itemArr[1]);
                                    break;
                                case "15":
                                case "OnCheck4GModuleParam":
                                    deviceRstTypeCnt.OnCheck4GModuleParam = int.Parse(itemArr[1]);
                                    break;
                                case "16":
                                case "OnCheckLBS":
                                    deviceRstTypeCnt.OnCheckLBS = int.Parse(itemArr[1]);
                                    break;
                                case "17":
                                case "OnModeToAuto":
                                    deviceRstTypeCnt.OnModeToAuto = int.Parse(itemArr[1]);
                                    break;
                                case "18":
                                case "GprsModule":
                                    deviceRstTypeCnt.GprsModule = itemArr[1];
                                    break;
                                case "19":
                                case "Len":
                                    deviceRstTypeCnt.Len = int.Parse(itemArr[1]);
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (System.Exception ex)
                        {
                            var msg = ex.Message;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 写入数据库
        /// </summary>
        /// <param name="dataArr"></param>
        public void Run(string[] dataArr)
        {
            Split(dataArr);
            mongoDBCommand.InsertDocumentAsync(deviceRstTypeCnt).GetAwaiter().GetResult(); 
        }
    }
}
